<?php


namespace app\home\controller;


use app\base\BaseController;
use think\facade\Db;
use think\facade\View;

class OrderPrefix extends BaseController
{
    protected $title='订单前缀';
    protected $table='num_prefix';
    public function index()
    {
        if (request()->isAjax()) {
            $param=$this->param;
            $where[]=['status','>',-1];
            if (isset($this->param['status'])&&$this->param['status']){
                $where[]=['status','=',$this->param['status']];
            }
            if (isset($this->param['name'])&&$this->param['name']){
                $where[]=['name','like','%'.$this->param['name'].'%'];
            }
            $rows = empty($param['limit']) ? get_config('app.page_size') : $param['limit'];
            $list = Db::name($this->table)
                ->where($where)
                ->field('*')
                ->paginate($rows);
            return table_assign(0, '', $list);
        } else {
            return view();
        }
    }
    public function add()
    {
        $param = get_params();
        if (request()->isAjax()) {
            $param=$this->param;
            $param['update_time']=time();

            if ($param['id'] > 0) {

                Db::name($this->table)->strict(false)->field(true)->update($param);
                add_log('edit', $param['id'], $param,$this->title);
            } else {
                $param['create_time'] = time();
                $rid = Db::name($this->table)->strict(false)->field(true)->insertGetId($param);
                add_log('add', $rid, $param,$this->title);
            }
            return to_assign();
        } else {
            $id = isset($param['id']) ? $param['id'] : 0;
            if ($id > 0) {
                $detail = Db::name($this->table)->where('id', $id)->find();
                View::assign('detail', $detail);
            }
            View::assign('id', $id);

            return view('form');
        }
    }
    public function delete()
    {

        if (request()->isDelete()) {
            $id = get_params("id");
            try {
                Db::name($this->table)->where('id','in',$id)->update(['status'=>-1,'deleted'=>time()]);
                add_log('delete', $id, request()->param(),$this->title);
            }catch (\Exception $e){
                return to_assign(1, "删除失败".$e->getMessage());
            }
            return to_assign(0, "删除成功");
        } else {
            return to_assign(1, "错误的请求");
        }
    }
}